package com.foyekoo.project001.utils;

import android.content.Intent;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;

import com.foyekoo.project001.App;

public class ReadThread extends BaseThread {
    private UsbDeviceConnection connection;
    private UsbEndpoint[][] endpoint;

    public static final String ACTION_DATA_RECIEVE = "com.android.usbhost.ACTION_DATA_RECEIVER";

    public static final String EXTRA_DATA = "com.android.usbhost.EXTRA_DATA";

    public void setConnection(UsbDeviceConnection connection, UsbEndpoint[][] point) {
        this.connection = connection;
        this.endpoint = point;
    }

    @Override
    public void run() {
        super.run();
        while(isRunning){
            try {


                byte[] readBuf = new byte[8];
                int  dataLength = connection.bulkTransfer(endpoint[1][1], readBuf, readBuf.length, 30);



                if (dataLength > 0) {
                    //TODO 广播数据出去

                    LogUtil.d("ReadThread",ByteUtil.byte2String(readBuf," "));

                    Intent intent = new Intent(ACTION_DATA_RECIEVE);
                    intent.putExtra(EXTRA_DATA,readBuf);
                    App.getInstance().sendBroadcast(intent);

                }
            } catch (Exception e) {
                e.printStackTrace();

            }

        }
    }
}
